Calculate Duration between two dates using Asp.Net C#
Hi everyone in this article, I’m explaining about calculate the
duration between two dates.
Description:
I was looking for a Duration calculator between two dates which gives duration in number of years, number of months and number of days together.
It can be defined as Age Calculator which is able to give any one’s exact age in years, months and days.
But unfortunately, I didn't find exactly what I was looking for. Most of the calculators give output either in years or in months or
in weeks or in days and so on and even in milliseconds, but not all the information together.
I realize that there should be something to calculate duration between two dates and give output in years, months and days together.
Step 1
Open visual studio >> New >> File >> Website
Give Application Name “DurationCalculate”
Step 2
Add a WebForms in your project
Your aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="background-color: #ed9a33">
<form id="form1" runat="server">
<table style="padding: 30px; color: White; margin-top: 30px">
<tr>
<td>From Date
</td>
<td>
<asp:TextBox ID="FromYearTxt" runat="server" OnTextChanged="FromYearTxt_TextChanged"></asp:TextBox>
</td>
<td>To Date
</td>
<td>
<asp:TextBox ID="ToYearTxt" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<div style="margin-left: 180px">
<asp:Button ID="btncalculate" runat="server" Text="Calculate" OnClick="btncalculate_Click" />
</div>
<table style="background-color: #9d84cc; color: White; padding: 30px; margin-top: 20px; margin-left: 15px"
id="tblResults" runat="server">
<tr>
<td>Years
</td>
<td id="tdYear" runat="server"></td>
</tr>
<tr>
<td>Total Months
</td>
<td id="tdMonths" runat="server"></td>
</tr>
<tr>
<td>Total Days
</td>
<td id="tdDays" runat="server"></td>
</tr>
<tr>
<td>Total Hours
</td>
<td id="tdHrs" runat="server"></td>
</tr>
<tr>
<td>Total Minutes </td>
<td id="tdminuts" runat="server"></td>
</tr>
<tr>
<td>Total Seconds </td>
<td id="tdseconds" runat="server"></td>
</tr>
<tr>
<td>Total MileSesonds </td>
<td id="tdmileSec" runat="server"></td>
</tr>
</table>
</form>
</body>
</html>
Preview
Now switch to design mode; it will look such as follows.
Now open the default.aspx.cs page and add the following code in the calculate button click event.
Read two input TextBox texts and assign them to a DateTime data type variable as in the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
tblResults.Visible = false;
}
protected void btncalculate_Click(object sender, EventArgs e)
{
if (FromYearTxt.Text != "" && ToYearTxt.Text != "")
{
DateTime FromYear = Convert.ToDateTime(FromYearTxt.Text);
DateTime ToYear = Convert.ToDateTime(ToYearTxt.Text);
TimeSpan objTimeSpan = ToYear - FromYear;
int Years = ToYear.Year - FromYear.Year;
int month = ToYear.Month - FromYear.Month;
double Days = Convert.ToDouble(objTimeSpan.TotalDays);
int TotalMonths = (Years * 12) + month;
double TotalHours = objTimeSpan.TotalHours;
double TotalMinutes = objTimeSpan.TotalMinutes;
double TotalSeconds = objTimeSpan.TotalSeconds;
double TotalMileSeconds = objTimeSpan.TotalMilliseconds;
tdYear.InnerText = Years + " Year " + month + " Months";
tdMonths.InnerText = Convert.ToString(TotalMonths);
tdDays.InnerText = Convert.ToString(Days);
tdHrs.InnerText = Convert.ToString(TotalHours);
tdminuts.InnerText = Convert.ToString(TotalMinutes);
tdseconds.InnerText = Convert.ToString(TotalSeconds);
tdmileSec.InnerText = Convert.ToString(TotalMileSeconds);
tblResults.Visible = true;
}
}
}
Now enter a From date and To date using the calendar as in the following:
Output
Now from all the preceding examples, we have learned how to calculate the years, months, days, minutes, seconds & milliseconds between two dates
In my next post, I’ll explain about Implement Captcha in ASP.NET MVC4
Leave Comment